// pages/release/release.js
import time from '../../utils/getTime'
let db = wx.cloud.database();
let userDB = db.collection('user');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName: '',//待发布的内容
    fileList: [],//保存临时图片
    userInfo: {}//保存用户信息

  },
  //   更新信息
  update() {
    let { nickName } = this.data;
    let { userInfo } = this.data;
    let { fileList } = this.data;
    if (nickName) {
      userInfo.nickName = nickName;
      this.setData({
        userInfo
      })
      if (fileList.length) {
        this.uploadToCloud();
      } else {
        userDB.where({
          phone: userInfo.phone
        }).update({
          data: {
            nickName: userInfo.nickName
          },
          success: res => {
            wx.showToast({
              title: '修改成功',
              icon: 'none'
            })
            wx.setStorageSync('userInfo', userInfo)
            wx.reLaunch({
              url: '../../pages/wode/wode',
            })
          }
        })

      }
    } else {
      if (fileList.length) {
        this.uploadToCloud();
      } else {
        wx.showToast({
          title: '请输入要修改的信息',
          icon: 'none'
        })
      }
    }

  },
  afterRead(event) {
    const { file } = event.detail;
    let { fileList } = this.data;
    fileList.push(file);
    this.setData({
      fileList
    })
  },
  uploadToCloud() {
    wx.cloud.init();
    const { fileList } = this.data;
    if (!fileList.length) {
      wx.showToast({ title: '请选择图片', icon: 'none' });
    } else {
      const uploadTasks = fileList.map((file, index) => this.uploadFilePromise(`my-photo${index}.png`, file));
      Promise.all(uploadTasks)
        .then(data => {
          wx.showToast({ title: '上传成功', icon: 'none' });
          const newFileList = data.map(item => ({ url: item.fileID }));
          this.setData({ cloudPath: data, fileList: newFileList });
          let { userInfo } = this.data;
          userInfo.avatarUrl = this.data.fileList[0].url;
          userDB.where({
            phone: userInfo.phone
          }).update({
            data: {
              nickName: userInfo.nickName,
              avatarUrl: userInfo.avatarUrl
            },
            success: res => {
              wx.showToast({
                title: '修改成功',
                icon: 'none'
              })
              wx.setStorageSync('userInfo', userInfo)
              wx.reLaunch({
                url: '../../pages/wode/wode',
              })
            }
          })
        })
        .catch(e => {
          wx.showToast({ title: '上传失败', icon: 'none' });
          console.log(e);
        });
    }
  },

  uploadFilePromise(fileName, chooseResult) {
    return wx.cloud.uploadFile({
      cloudPath: 'i_chat/images/user/' + chooseResult.size+'post'+fileName,
      filePath: chooseResult.url
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.cloud.init();
    let userInfo = wx.getStorageSync('userInfo');
    let phone = wx.getStorageSync('phone');
    userInfo.phone = phone;
    this.setData({
      userInfo
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})